/*
 * @Date: 2022-02-14 11:24:10
 * @LastEditors: 春贰
 * @gitee: https://gitee.com/chun22222222
 * @github: https://github.com/chun222
 * @Desc:
 * @LastEditTime: 2022-03-17 10:22:28
 * @FilePath: \gin-antd\server\system\controller\sys\role\Role.go
 */
package role

import (
	"github.com/gin-gonic/gin"
	"go-easy-dashboard/system/core/request"
	"go-easy-dashboard/system/core/response"
	"go-easy-dashboard/system/model/CommonModel"
	"go-easy-dashboard/system/model/DbModel"
	"go-easy-dashboard/system/model/RequestModel"
	"go-easy-dashboard/system/service/SysService"
	"go-easy-dashboard/system/util/dict"
)

var RoleService SysService.RoleService

/**
*
* @api {post} /user/list 用户列表
* @apiName 用户列表
* @apiGroup 用户
* @apiVersion  1.0.0
*
* @apiBody  {String}  page
* @apiBody  {String}  pageSize
* @apiSampleRequest /user/list
* @apiHeader {String} Authorization 唯一token
*
* @apiParamExample  {json} Request-Example:
* {
*    key : "value",
* }
*
*
* @apiSuccessExample {json} Success-Response:
*
{
 "code": 0,
	"data": {
	},
	"msg": "sucess"
}
*
*
*/
func List(c *gin.Context) {
	var r RequestModel.SysRoleSearch
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}

	if err, list, total := RoleService.List(r); err != nil {
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(
			CommonModel.PageResult{
				List:  list,
				Total: total,
			}, c)
	}

}

//获取已分配权限
func Permissions(c *gin.Context) {
	var r CommonModel.Id
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}

	if err, list := RoleService.Permissions(r.Id); err != nil {
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(
			list, c)
	}
}

//分配权限
func SetPermissions(c *gin.Context) {
	var r RequestModel.SysSetPermissions
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}

	if err := RoleService.SetPermissions(r); err != nil {
		response.FailWithMessage(err.Error(), c)
	} else {
		response.Ok(c)
	}
}

/**
*
* @api {post} /user/add 新增用户
* @apiName 新增用户
* @apiGroup 用户
* @apiVersion  1.0.0
* @apiHeader {String} Authorization 唯一token
* @apiBody  {String}  apiBody
* @apiSampleRequest /user/add
*
* @apiParamExample  {json} Request-Example:
*{"login_name":"12s1","User_Name":"asss","password":"ad","email":"sss@qq.com"}
*
*
* @apiSuccessExample {json} Success-Response:
*
{
    "code": 0,
    "data": 12,  //用户id
    "msg": "success"
}
*
*
*/
func Add(c *gin.Context) {
	var r DbModel.SysRole
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}

	d := DbModel.SysRole{}
	dict.StructAssign(&d, &r) //批量传值
	if err = RoleService.Add(&d); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	} else {
		response.OkWithData(d.Name, c)
	}
}

func Edit(c *gin.Context) {
	var r DbModel.SysRole
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}

	if err = RoleService.Update(&r); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	} else {
		response.Ok(c)
	}
}

func Romove(c *gin.Context) {
	var r CommonModel.Ids
	err, msg := request.Binding(&r, c)
	if err != nil {
		response.FailWithMessage(msg, c)
		return
	}

	if err = RoleService.Remove(r); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	} else {
		response.Ok(c)
	}
}
